﻿/*
  +++++++++++++++++++++++++++++++++++++++++++
  +  Description: 字典数据表 管理
  +  Created by 王大宸 on 2020-10-05 23:28:38
  +  Created with IntelliJ IDEA.
  ++++++++++++++++++++++++++++++++++++++++++++
 */

var path = "/base/dict/data";
layui.use(['table', 'laypage'], function () {
    table = layui.table;
    layPage = layui.laypage;
    form = layui.form;

    /* 初始化表格 */
    initTable();

    // 搜索
    $.searchTable.searchBtn();

    //监听冻结操作
    form.on('checkbox(status)', function (obj) {
        onStatus(this.value, obj.elem.checked);
    });

    //监听行工具事件
    table.on('tool(baseDictDataLay)', function (obj) {
        let data = obj.data;
        if ('del' === obj.event) {
            let removeUrl = path + '/' + data.id;
            $.http.remove(removeUrl, data.dictLabel);
        } else if ('edit' === obj.event) {
            let editUrl = path + '/edit/' + data.id;
            $.modal.open('修改', editUrl, '600', '430');
        } else if ('see' === obj.event) {
            let seeUrl = path + '/see/' + data.id;
            $.modal.see('查看', seeUrl, '600', '350');
        }
    });

});

function goBack() {
    window.history.go(-1);
}


/* 加载表格 */
function initTable() {
    let name = $("#name").val();
    let dictType = $("#dictType").val();
    $.table.init({
        url: path + "/page",
        where: {
            dictLabel: name,
            dictType: dictType
        },
        cols: [[
            {field: 'dictLabel', title: '字典标签', align: "center"},
            {field: 'dictValue', title: '字典键值', align: "center"},
            {field: 'dictSort', title: '字典排序', align: "center"},
            {field: 'status', title: '状态', align: "center", templet: '#status'},
            {field: 'createUser', title: '创建者', align: "center"},
            {field: 'createTime', title: '创建时间', align: "center"},
            {field: 'updateUser', title: '更新者', align: "center"},
            {field: 'updateTime', title: '更新时间', align: "center"},
            {fixed: 'right', title: '操作', toolbar: '#toolbarHandle', width: 180, align: "center"}
        ]],
    });
}

/* 添加按钮 */
function saveBaseDictData() {
    let dictType = $("#dictType").val();
    let addUrl = path + '/' + dictType + '/add';
    $.modal.open("添加", addUrl, "600", "430");
}

/* 禁用 */
function onStatus(id, status) {
    let data = new Object();
    data.id = id;
    data.status = '1';
    if (status) {
        data.status = '0';
    }
    let url = path + '/status/' + id;
    $.http.putAjax(url, data, function (res) {
        if (WEB_STATUS.SUCCESS === res.status) {
            $.msg.msgSuccess(res.msg);
        }
        if (WEB_STATUS.WARNING === res.status) {
            initTable();
            $.msg.msgWarning(res.msg);
        }
        if (WEB_STATUS.ERROR === res.status) {
            initTable();
            $.msg.msgError("系统错误,请联系管理工作人员!");
        }
    })
}
